import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/address_picker/route/address_picker_route.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/default_style.dart';
import 'package:flutter_yishop/config/Config.dart';
import 'package:flutter_yishop/config/EventBus.dart';
import 'package:flutter_yishop/services/SignService.dart';
import 'package:flutter_yishop/services/UserService.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';
import 'package:flutter_yishop/utils/toast_util.dart';
import 'package:flutter_yishop/widget/JdButton.dart';
import 'package:flutter_yishop/widget/JdText.dart';

class AddressAdd extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AddressAddState();
  }
}

class _AddressAddState extends State<AddressAdd> {
  String area = '';
  String name = '';
  String phone = '';
  String address = '';
  String province="";
  String city="";
  String county = "";
  String detailsAddress = "";

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("增加收货地址"),
      ),
      body: Container(
        padding: EdgeInsets.all(10),
        child: ListView(
          children: <Widget>[
            SizedBox(height: 20),
            JdText(
              text: "收货人姓名",
              onChanged: (value) {
                name = value;
              },
            ),
            SizedBox(height: 10),
            JdText(
              text: "收货人电话",
              onChanged: (value) {
                phone = value;
              },
            ),
            SizedBox(height: 10),
            Container(
              padding: EdgeInsets.only(left: 5),
              height: ScreenAdapter.height(68),
              decoration: BoxDecoration(
                  border: Border(
                      bottom: BorderSide(width: 1, color: Colors.black12))),
              child: InkWell(
                child: Row(
                  children: <Widget>[
                    Icon(Icons.add_location),
                    this.area.length > 0
                        ? Text('${this.area}',
                            style: TextStyle(color: Colors.black54))
                        : Text('省/市/区', style: TextStyle(color: Colors.black54))
                  ],
                ),
                onTap: () async {
                  Pickers.showAddressPicker(context,
                      initTown: "",
                      pickerStyle: RaisedPickerStyle(), onConfirm: (p, c, t) {
                    setState(() {
                      this.area =
                          "${p == "全部" ? "" : p}${c == "全部" ? "" : c}${t == "全部" ? "" : t}";
                      // this.address =  "p:${p == "全部" ? "" : p}c:${c == "全部" ? "" : c}t:${t == "全部" ? "" : t}";
                      province = (p == "全部" ? "" : p);
                      city = (c == "全部" ? "" : c);
                      county = (t == "全部" ? "" : t);
                    });
                  });
                },
              ),
            ),
            SizedBox(height: 10),
            JdText(
              text: "详细地址",
              maxLines: 4,
              height: 200,
              onChanged: (value) {
                detailsAddress = value;
              },
            ),
            SizedBox(height: 10),
            SizedBox(height: 40),
            JdButton(
              text: "增加",
              color: Colors.red,
              cb: ()  {
               _addAddressRequest();
              },
            )
          ],
        ),
      ),
    );
  }

  _addAddressRequest()async{
    this.address =  "p:${province}c:${city}t:${county}a:$detailsAddress";
    // print('添加地址：$address');
    var tempJson = {
      "uid": UserService.getUserInfo()[0]["_id"],
      "name": name,
      "phone": phone,
      "address": address,
      "salt": UserService.getUserInfo()[0]["salt"]
    };

    var sign = SignService.getSign(tempJson);
    var api = "${Config.url}api/addAddress";
    var result = await Dio().post(api, data: {
      "uid": UserService.getUserInfo()[0]["_id"],
      "name": name,
      "phone": phone,
      "address": address,
      "sign": sign
    });
    if(result.data["success"]){
      Navigator.pop(context);
    }else{
      ToastUtil.showToasts(result.data["message"]);
    }
  }


  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    eventBus.fire(new AddressEvent("增加成功"));
    eventBus.fire(new CheckoutEvent("修改成功"));
  }
}
